﻿Public Class CLabelSet_Screen2 : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CLabelSet_Screen2.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABEL_CHECK_MODE") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' ラベル照合絞るリスト画面
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase = Nothing
        Dim pdaTask As CPDATask2 = DirectCast(task, CPDATask2)
        Dim webMng As CWebManager = CWebManager.GetInstance
        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance

        'Dim waitTask As CPersonal = New CPersonal(pdaTask)
        Debug.WriteLine("CLabelSet_Screen2.DoExecute...START")

        Dim StateDao As CMasterDAO = New CMasterDAO
        Dim app As CApplication = CApplication.GetInstance()
        Dim masterDao As New CMasterDAO
        Dim sql As CSQLite = app.MasterDB()
        Dim trx As CTransaction = sql.GetTransaction

        Dim delRecord As ArrayList = New ArrayList

        Try
            '読んだラベルをチェック
            Dim retSetCdLst As ArrayList = StateDao.GetSetCdList(trx, task.ProcessText)

            'ラベル照合リストを絞った事があれば検査
            '絞った時点で他のラベルセットを読んだらエラーになる
            '最初の時点に戻す
            Select Case CEventCheck.LabelListSetCdChk(pdaTask, retSetCdLst)

                '別ラベル照合を読んだらエラー
                Case 0
                    '初期化-------------------------->>START
                    CEventCheck.initErrPdaTask(pdaTask)
                    '初期化-------------------------->>END
                    ''出荷情報のリストに戻す
                    pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.LIST_MODE)
                    'エラーメッセージ表示
                    CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                        Color.Black, CConst.EnumSound.ALERT_BUZZ, "別のラベルセットです。", Color.White)


                Case 1
                    '正常

                    Dim cntCount As Integer = 0
                    Dim cntSetCd As String = ""

                    If pdaTask.LabelCheckList.Count > 1 And retSetCdLst.Count > 1 Then
                        Dim cntFlg As Boolean = False
                        For i As Integer = 0 To pdaTask.LabelCheckList.Count - 1
                            Dim labelLstSetCd As String = pdaTask.LabelCheckList(i)
                            For j As Integer = 0 To retSetCdLst.Count - 1
                                If labelLstSetCd = retSetCdLst(j) Then
                                    cntFlg = True
                                    Exit For
                                End If
                            Next
                        Next
                        If Not cntFlg Then
                            'エラー
                            '初期化-------------------------->>START
                            CEventCheck.initErrPdaTask(pdaTask)
                            '初期化-------------------------->>END
                            'MessageBox.Show("0")
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.LIST_MODE)
                            'エラーメッセージ表示
                            CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                        End If

                    ElseIf pdaTask.LabelCheckList.Count > 1 Then
                        'MessageBox.Show("チェック")
                        Dim cntFlg As Boolean = False
                        Dim delList As ArrayList = New ArrayList
                        For i As Integer = 0 To pdaTask.LabelCheckList.Count - 1
                            Dim labelLstSetCd As String = pdaTask.LabelCheckList(i)
                            For j As Integer = 0 To retSetCdLst.Count - 1
                                If labelLstSetCd = retSetCdLst(j) Then
                                    cntCount += 1
                                    cntSetCd = labelLstSetCd
                                    cntFlg = True
                                    Exit For
                                End If
                            Next
                            '削除するもの
                            If Not cntFlg Then
                                delList.Add(labelLstSetCd)
                            End If
                        Next
                        '絞ったリストから削除
                        For h As Integer = 0 To delList.Count - 1
                            pdaTask.LabelCheckList.Remove(delList(h))
                        Next

                        For i As Integer = 0 To pdaTask.WindowManager.GetLabelCheckCount - 1
                            Dim shpOrderData As COrderRecord = pdaTask.WindowManager.GetRecord(i, CBaseRecord.EnumRecordType.LabelCheck)
                            delRecord.Add(shpOrderData)
                        Next
                        For j As Integer = 0 To delRecord.Count - 1
                            pdaTask.WindowManager.DelRecord(delRecord(j), True)
                        Next

                        If cntCount = 1 Then
                            pdaTask.SetCd = cntSetCd
                            If pdaTask.SetCd <> "" And pdaTask.OrderCd <> "" Then
                                '注文のデータと読んだラベルと比べる
                                If CPersonal.ChkOrderDataSetCd(pdaTask) Then
                                    'INSERTする前確認
                                    If CPersonal.INSERT_ADD_LABEL(pdaTask) Then
                                        '初期化-------------------------->>START
                                        'CEventCheck.initPdaTask(pdaTask)
                                        'MessageBox.Show("INSERT後の初期化が必要")
                                        '初期化-------------------------->>END
                                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.LIST_MODE)
                                        '完全に照合成功
                                        pdaTask.Hardware.nextC()
                                    Else
                                        'INSERT失敗
                                        '既に使用済みのもの
                                        '初期化-------------------------->>START
                                        CEventCheck.initErrPdaTask(pdaTask)
                                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.LIST_MODE)

                                    End If
                                Else
                                    '先、注文番号を選択して他のラベル読んだ時点
                                    '選択解除
                                    Dim listCount As Integer = pdaTask.WindowManager.GetRecordCount()
                                    For cn As Integer = 0 To listCount - 1
                                        Dim setline As COrderRecord = pdaTask.WindowManager.GetRecord(cn)
                                        If setline.OrderCd = pdaTask.OrderCd Then
                                            setline.RowColor = Color.White
                                            setline.IsSelectFlg = False
                                            Exit For
                                        End If
                                    Next
                                    '初期化-------------------------->>START
                                    CEventCheck.initErrPdaTask(pdaTask)
                                    '初期化-------------------------->>END
                                    'MessageBox.Show("1")
                                    pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.LIST_MODE)
                                    'エラーメッセージ表示
                                    CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                        Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                                End If
                            Else
                                '出荷照合絞ったデータ画面
                                If pdaTask.OrderCdSet.Count > 1 Then
                                    '選択されたすぐ、照合する
                                    CCommon.FlagManage("ORDER_COMMIT", True)
                                    pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.REPORTLIST_MODE)
                                Else
                                    'MessageBox.Show("1-1")
                                    '初期化-------------------------->>START
                                    CEventCheck.initErrPdaTask(pdaTask)
                                    '初期化-------------------------->>END
                                    pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.LIST_MODE)
                                    'エラーメッセージ表示
                                    CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                        Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)

                                End If
                            End If

                        ElseIf cntCount > 1 Then
                            'まだ、ラベルが複数が存在ので絞った画面


                        Else
                            'エラー
                            'MessageBox.Show("1-2")
                            '初期化-------------------------->>START
                            CEventCheck.initErrPdaTask(pdaTask)
                            '初期化-------------------------->>END
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.LIST_MODE)

                            'エラーメッセージ表示
                            CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)

                        End If
                    End If

            End Select

        Catch ex As Exception
            'MessageBox.Show("CLABELエラー:" & ex.Message)
        End Try

        task.ActionCd = "LABEL"
        task.ProcessText = ""
        nextState = GetNextState(task)
        Return nextState

    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CLabelSet_Screen2.DoExit")
        Return True
    End Function


End Class
